﻿using AutoMapper;
using MediatR;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.Domains.BatchManagementFiles;
using Traceability.Commont.DTOs.BatchManagementDTOs;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.BatchManagements
{
    public class BatchSlaugherListHander : IRequestHandler<BatchSlaugherListCommand, PageDTO<BatchSlaugherListDTO>>
    {
        private readonly IBateRepository<Approach> appro;
        private readonly IBateRepository<LiveAnimalweighing> live;
        private readonly IBateRepository<SlaughterCircle> slaughterCircle;
        private readonly IBateRepository<BulkSlaughter> bulkSlaughter;
        private readonly IMapper mapper;

        public BatchSlaugherListHander(IBateRepository<Approach> appro, IBateRepository<LiveAnimalweighing> live, IBateRepository<SlaughterCircle> slaughterCircle, IBateRepository<BulkSlaughter> bulkSlaughter, IMapper mapper)
        {
            this.appro = appro;
            this.live = live;
            this.slaughterCircle = slaughterCircle;
            this.bulkSlaughter = bulkSlaughter;
            this.mapper = mapper;
        }

        public async Task<PageDTO<BatchSlaugherListDTO>> Handle(BatchSlaugherListCommand request, CancellationToken cancellationToken)
        {
            try
            {
                PageDTO<BatchSlaugherListDTO> res = new PageDTO<BatchSlaugherListDTO>() { totalCount = 0, pageCount = 0 };
                BatchSelectDTO batch = mapper.Map<BatchSelectDTO>(request);
                var list = (from d in bulkSlaughter.GetAll()
                           //join b in live.GetAll() on a.ApproachId equals b.ApproachId
                           //join c in slaughterCircle.GetAll() on b.SlaughterCircleId equals c.SlaughterCircledId
                           //join d in bulkSlaughter.GetAll() on b.LiveAnimalweighingId equals d.LiveAnimalweighingId
                           select new BatchSlaugherListDTO()
                           {
                               BulkSlaughterId=d.BulkSlaughterId,
                               DocumentsTime=d.DocumentsTime,
                               DocumentsNo=d.DocumentsNo,
                               AccountingUnit= d.AccountingUnit,
                               ProductionCrews=d.ProductionCrews,
                               DateOfManufacture = d.DateOfManufacture,
                               BulkSlaughterNum = d.BulkSlaughterNum,
                               MakeASingle = d.MakeASingle,
                               CreateThisTime = d.CreateThisTime,
                               Stact=d.Stact,
                           });
                if (batch.BeginTime!=null)
                {
                    list = list.Where(x=>x.CreateThisTime <= batch.BeginTime);
                }
                if (batch.EndTime != null)
                {
                    list = list.Where(x => x.CreateThisTime >= batch.EndTime);
                }
                if (!string.IsNullOrEmpty(batch.DocumentsNo))
                {
                    list = list.Where(x => x.DocumentsNo.Contains(batch.DocumentsNo));
                }
                if (batch.BeginProduceTime != null)
                {
                    list = list.Where(x => x.DateOfManufacture <= batch.BeginProduceTime);
                }
                if (batch.EndProduceTime != null)
                {
                    list = list.Where(x => x.DateOfManufacture >= batch.EndProduceTime);
                }
                if (!string.IsNullOrEmpty(batch.Team))
                {
                    list = list.Where(x => x.ProductionCrews.Contains(batch.Team));
                }
                if (!string.IsNullOrEmpty(batch.MakeASingle))
                {
                    list = list.Where(x => x.MakeASingle.Contains(batch.MakeASingle));
                }
                if (batch.Stact > -1)
                {
                    list = list.Where(x => x.Stact == batch.Stact) ;
                }

                res.totalCount = list.Count();
                res.pageCount =(int) Math.Ceiling(list.Count()*1.0/batch.pageSize);
                list = list.OrderBy(x => x.BulkSlaughterId).Skip((batch.pageIndex - 1) * batch.pageSize).Take(batch.pageSize);
                res.Lists = list.ToList();
                return  res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
